package com.conorroche.taskmanagement;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TaggedTasksActivity extends Activity implements ActionBar.TabListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tagged_tasks);
		// Set up the action bar to show tabs.
				actionBar = getActionBar();
				actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

				// For each of the sections in the app, add a tab to the action bar.
				actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tasks)
						.setTabListener(this));
				actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tags)
						.setTabListener(this));
				actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_location)
						.setTabListener(this));
				actionBar.setSelectedNavigationItem(1);
		
		
		ListView tasksList = (ListView) findViewById(R.id.activity_tasks_tagsList);
		
		extras = getIntent().getExtras();
		Log.d("TagsActivity", extras.getString("clicked"));
		DBAdapter db = new DBAdapter(getApplicationContext());
		db.open();
		Cursor taggedTasks = db.getTaggedTasks(extras.getString("clicked"));
		taggedTasks.moveToFirst();
		int c = 0;
		tasks = new String[taggedTasks.getCount()];
		taskIds = new int[taggedTasks.getCount()];
		taggedTasks.moveToFirst();
		while(!taggedTasks.isAfterLast()){
			tasks[c] = taggedTasks.getString(taggedTasks.getColumnIndex("name"));
			taskIds[c] = taggedTasks.getInt(taggedTasks.getColumnIndex("_id"));
			//Log.d("Tags Helper: Tag Id", tasks[c]);
			c++;
			taggedTasks.moveToNext();
		}
		db.close();
		adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, tasks);
		tasksList.setAdapter(adapter);
		tasksList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showTask = new Intent(getApplicationContext(), ViewTaskActivity.class);
				showTask.putExtra("id", taskIds[arg2]);
				startActivity(showTask);
			}
		});
		
	}
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
	}
	@Override
	public void onSaveInstanceState(Bundle outState) {
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_tags, menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.menu_delete:
        		DBAdapter db = new DBAdapter(getApplicationContext());
        		db.open();
        		db.deleteTag(extras.getInt("clicked"));
        		db.close();
        		finish();
        }
        return super.onOptionsItemSelected(item);
    }
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		actionBar.setSelectedNavigationItem(1);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		switch(tab.getPosition()){
			case 0:	// all tasks
				if(first){
					first = false;
					//actionBar.setSelectedNavigationItem(1);
				}else{
					Intent allTasks = new Intent(getApplicationContext(), MainActivity.class);
					startActivity(allTasks);
				}
			break;
			case 1: //all tags
			break;
			case 2:
				//do something
				Intent map = new Intent(getApplicationContext(), LocationActivity.class);
				startActivity(map);
			break;
		}
		
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		
	}
	private String tasks[];
	private static int[] taskIds;
	private ArrayAdapter<String> adapter;
	private Intent showTask;
	private Bundle extras;
	public ActionBar actionBar;
	boolean first = true;
}

